#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>

#define MULTICAST_GROUP "224.1.1.1"
#define PORT 5000
#define BUFFER_SIZE 1024 * 1024

int main()
{
    int sockfd;
    struct sockaddr_in multicast_addr;
    char buffer[BUFFER_SIZE];
    int file_fd;
    ssize_t bytes_read;

    /* 创建 UDP 套接字 */
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    /* 设置多播地址 */
    memset(&multicast_addr, 0, sizeof(multicast_addr));
    multicast_addr.sin_family = AF_INET;
    multicast_addr.sin_port = htons(PORT);
    inet_pton(AF_INET, MULTICAST_GROUP, &multicast_addr.sin_addr);

    /* 打开要发送的文件 */
    if ((file_fd = open("large_file.bin", O_RDONLY)) < 0)
    {
        perror("file open failed");
        close(sockfd);
        exit(EXIT_FAILURE);
    }

    /* 读取文件并发送 */
    while ((bytes_read = read(file_fd, buffer, BUFFER_SIZE)) > 0)
    {
        if (sendto(sockfd, buffer, bytes_read, 0, (struct sockaddr *)&multicast_addr, sizeof(multicast_addr)) < 0)
        {
            perror("sendto failed");
            break;
        }
    }

    close(file_fd);
    close(sockfd);
    return 0;
}